package com.work.farm.service.impl;

import com.alibaba.fastjson.JSON;
import com.work.crops.feign.CropsIndexFeign;
import com.work.crops.feign.CropsInfoFeign;
import com.work.crops.pojo.CropsInfo;
import com.work.crops.pojo.CropsInfoIndex;
import com.work.farm.dao.FarmLandCropsDao;
import com.work.farm.pojo.TCustFarmlandCropsEntity;
import com.work.farm.service.FarmLandCropsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class FarmLandCropsServiceImpl implements FarmLandCropsService {

    @Autowired
    private FarmLandCropsDao farmLandCropsDao;

    @Autowired
    private CropsInfoFeign cropsInfoFeign;

    @Autowired
    private CropsIndexFeign cropsIndexFeign;

    /**
     * 种植农作物
     * @param tCustFarmlandCropsEntity
     */
    @Override
    public void addFarmCrops(TCustFarmlandCropsEntity tCustFarmlandCropsEntity) {
        //获取该农作物所有的生长阶段
        CropsInfo cropsInfo = cropsInfoFeign.findCropsById(Integer.parseInt(tCustFarmlandCropsEntity.getCid().toString())).getData();
        String stage = cropsInfo.getStage();
        String[] strings = JSON.parseObject(stage, String[].class);
        //设置农作物的生长阶段
        tCustFarmlandCropsEntity.setStage(strings[0]);
        //设置当前时间为种植农作物的起始时间
        tCustFarmlandCropsEntity.setStartTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        //添加农作物
        farmLandCropsDao.insertSelective(tCustFarmlandCropsEntity);
    }

    /**
     * 农作物详情
     * @param fid
     * @return
     */
    @Override
    public Map<String,Object> findById(Integer fid) {
        //创建一个map用于返回
        Map<String, Object> map = new HashMap<>();
        //根据农作物id查询农作物详情
        CropsInfo cropsInfo = cropsInfoFeign.findCropsById(fid).getData();
        //得到农作物的生长状态
        String stage = cropsInfo.getStage();
        String[] strings = JSON.parseObject(stage, String[].class);
        for (String string : strings) {
            //根据农作物id和生长状态进行查询指标
            List<CropsInfoIndex> cropsInfoIndices = cropsIndexFeign.findById(cropsInfo.getId(), string).getData();
            //指标map，用于存放指标数据
            List<Map<String,String>> list=new ArrayList<>();
            for (CropsInfoIndex cropsInfoIndex : cropsInfoIndices) {
                Map<String, String > map1 = new HashMap<>();
                map1.put("range",cropsInfoIndex.getOptRange());
                //添加指标数据
                map1.put("indexType","水");
                map1.put("indexRange","40%");
                list.add(map1);
            }
            map.put("index",list);
        }

        return map;
    }

    /**
     * 修改农作物状态信息
     * @param fid
     * @param stage
     */
    @Override
    public void updateStatus(Integer fid, String stage) {
        //根据id查询
        TCustFarmlandCropsEntity tCustFarmlandCropsEntity = farmLandCropsDao.selectByPrimaryKey(fid);
        //定义一个字符串
        String endTime = null;
        //判断是否是成熟期
        if ("成熟期".equals(stage)){
            //设置结束时间
            endTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        }
        tCustFarmlandCropsEntity.setStage(stage);
        tCustFarmlandCropsEntity.setEndTime(endTime);
        //更新表状态信息
        farmLandCropsDao.updateByPrimaryKeySelective(tCustFarmlandCropsEntity);
    }


}
